//
//  UIStandardBlockView.m
//  vhStressECG
//
//  Created by jia yu on 2021/10/26.
//

#import "UIStandardBlockView.h"
#import <vhECGTools/vhECGTools.h>


@interface UIStandardBlockView(){
    float screenRatio;
    UILabel *nameLabel;
    UIView *blockView;
}
@end

@implementation UIStandardBlockView
@synthesize blockName;
@synthesize enable;

-(void)setEnable:(BOOL)_enable{
    enable = _enable;
    for (UIView *view in self.subviews){
        if ([view isKindOfClass:[UIStandardButton class]]){
            UIStandardButton *b = (UIStandardButton *)view;
            b.enabled = enable;
            continue;
        }
        if ([view isKindOfClass:[UIStandardLabel class]]){
            UIStandardLabel *l = (UIStandardLabel *)view;
            l.enabled = enable;
            continue;
        }
        if ([view isKindOfClass:[UIStandardSelectButton class]]){
            UIStandardSelectButton *b = (UIStandardSelectButton *)view;
            b.enabled = enable;
            continue;
        }
    }
}

-(void)setBlockName:(NSString *)_blockName{
    blockName = _blockName;
    nameLabel.text = blockName;
    CGSize sNeeded = [blockName sizeWithAttributes:@{NSFontAttributeName:nameLabel.font}];
    [nameLabel setFrame:CGRectIntegral(CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y, sNeeded.width + 20.f * screenRatio, nameLabel.frame.size.height))];
}

-(id)initWithFrame:(CGRect)frame{
    return [[UIStandardBlockView alloc] initWithFrame:frame scale:1.f];
}

-(id)initWithFrame:(CGRect)frame scale:(float)scale{
    self = [super initWithFrame:frame];
    if (self){
        screenRatio = scale;
        self.backgroundColor = [UIColor clearColor];
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        blockView = [[UIView alloc] initWithFrame:CGRectIntegral(CGRectMake(28.f * screenRatio, 75.f * screenRatio,
                                                             frame.size.width - 56.f * screenRatio,
                                                             frame.size.height-120.f * screenRatio))];
        blockView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        blockView.backgroundColor = [UIColor clearColor];
        blockView.layer.masksToBounds = YES;
        blockView.layer.cornerRadius = 10.f * screenRatio;
        blockView.layer.borderColor = [vhECGTools colorFromHEXString:@"ededed"].CGColor;
        blockView.layer.borderWidth = 3.f * screenRatio;
        [self addSubview:blockView];
        
        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(48.f * screenRatio, 50.f * screenRatio, 10.f, 42.f * screenRatio)];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:34.f * screenRatio];
        nameLabel.backgroundColor = [UIColor whiteColor];
        nameLabel.textColor = [vhECGTools colorFromHEXString:@"294559"];
        [self addSubview:nameLabel];
    }
    return self;
}


-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
}
@end
